﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace RoboTrocity {
	/// <summary>
	/// This represents the guns that the player can equip/upgrade
	/// </summary>
	class Gun {

		public delegate List<Bullet> GunShot(Bullet bullet);

		public GunShot shootGun;
		public Bullet shotType;
		private float cooldown;
		private float timeSinceLastShot;
		private bool unlocked;

		string name;

		static private List<Bullet> bullets;
		

		public Gun():this(SingleShot) {}

		public Gun(GunShot _shootGun) {
			cooldown = 0.1f;
			timeSinceLastShot = cooldown;
			bullets = new List<Bullet>();
			shootGun = _shootGun;
			shotType = new Bullet(new Vector2(1, 0), 5);
			name = "";
		}

		public List<Bullet> Shoot() {
			bullets.Clear();
			if (!IsReadyToFire){
				return bullets;
			} 
			timeSinceLastShot = 0.0f;

			return shootGun(shotType);
		}

		public static List<Bullet> SingleShot(Bullet bullet) {
			Bullet tempBullet = BulletPool.Instance.getNewBullet(bullet);
			tempBullet.Velocity = new Vector2(12, 0);
			bullets.Add(tempBullet);
		
			return bullets;
		}

		public static List<Bullet> TripleShot(Bullet bullet) {
			Bullet tempBullet = BulletPool.Instance.getNewBullet(bullet);
			tempBullet.Velocity = new Vector2(12, 8);
			bullets.Add(tempBullet);
			tempBullet = BulletPool.Instance.getNewBullet(bullet);
			tempBullet.Velocity = new Vector2(12, 0);
			bullets.Add(tempBullet);
			tempBullet = BulletPool.Instance.getNewBullet(bullet);
			tempBullet.Velocity = new Vector2(12, -8);
			bullets.Add(tempBullet);

			return bullets;
		}

		public void PassTimeSinceShot(float sinceLastShot) {
			timeSinceLastShot += sinceLastShot;
		}

		public float Cooldown {
			get { return cooldown; }
			set { cooldown = value; }
		}

		public bool IsReadyToFire {
			get { return (timeSinceLastShot >= cooldown ? true : false); }
		}

		public string Name {
			get { return name; }
			set { name = value; }
		}

		public override bool Equals(object obj) {
			if (obj is Gun) { 
				if (((Gun)obj).shotType == this.shotType) {
					return true;
				}
			}
			return false;
		}

		public bool isUnlocked {
			get { return unlocked; }
			set { unlocked = value; }
		}
	}
}
